<?php

/**
 * uniBoard ACP New User
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-20 21:23:27 +0000 (Mon, 20 Sep 2010) $
 * $Revision: 64 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_new_user{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * Our api
	 *
	 * @var object
	 */
	
	private $api			= NULL;
	
	/**
	 * Avaiable groups?
	 *
	 * @var array
	 */
	
	private $groups			= array();
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
		
		// Create API
		$this -> api = new api_user( $appCore);
		
		// Get avaiable groups
		$this -> groups = $appCore -> users -> getUserGroups();
			
		// Update?
		if ( $appCore -> _POST( 'update') == 1)
		{
			// Fill Api
			$this -> api -> setUserLogin( $appCore -> _POST( 'user_login'));
			$this -> api -> setUserName( $appCore -> _POST( 'user_name'));
			$this -> api -> setUserEmail( $appCore -> _POST( 'user_email'));
			$this -> api -> setUserPassword( $appCore -> _POST( 'user_password'));
			
			// User Goup all right?
			if ( isset( $this -> groups[$appCore -> _POST( 'user_group')]))
			{
				$this -> api -> setGroupId((integer) $appCore -> _POST( 'user_group'));
				$this -> api -> setUserGroups(array((integer) $appCore -> _POST( 'user_group')));
			}
			else
			{
				// Api error
				$this -> api -> setError( $appCore -> lang['acp_user_group_incorrect']);	
			}
			
			// Api All-rite?
			if ( $this -> api -> error)
			{
				// Draw Errors
				$appCore -> output -> error( join( '<br />', $this -> api -> errors), $appCore -> lang['acp_new_user']);
				
				// Repeat Form
				$this -> drawForm();
			}
			else
			{
				// Give API basic setup
				$this -> api -> setUserWantEmails( 1);
				$this -> api -> setUserShowEmail( 0);
				$this -> api -> setUserWantMailings( 1);
				
				$this -> api -> setUserTimezone( $appCore -> conf['board_timezone']);
				$this -> api -> setUserDST( $appCore -> conf['board_use_dst']);
				
				$this -> api -> setUserLanguage( $appCore -> conf['default_language']);
				$this -> api -> setUserStyle( $appCore -> conf['default_style']);
					
				// Set Activation Type to 0
				$this -> api -> setUserActivation( 0);
				
				// Fill First and Last time on-line
				$this -> api -> setUserNameDate( $appCore -> time);
				$this -> api -> setUserJoinDate( $appCore -> time);
				$this -> api -> setUserJoinIp( $appCore -> client_ip);
				$this -> api -> setUserLastDate( $appCore -> time);
				$this -> api -> setUserLastIp( $appCore -> client_ip);
							
				// Save Api
				$this -> api -> saveData();
				
				// Make him member of his group
				$appCore -> users -> makeMember( $this -> api -> group_id, $this -> api -> user_name);
				
				// Update Latest user
				$appCore -> users -> setLatestUser( $this -> api -> user_id, $this -> api -> user_name);
				
				// Set Log
				$appCore -> logAdmin( 'log_admin_created_new_user', htmlspecialchars( $this -> api -> user_name));
				
				// Redirect us back to list
				$appCore -> output -> drawRedirect( $appCore -> adminLink( 'act=users'), $appCore -> lang['acp_user_new_saved']);
				
				// Notive that dude?
				if ( $appCore -> _POST('user_notify'))
				{
					// Keep Script Alive
					$appCore -> stayAlive();
					
					// Start our great mailer
					$mailer = new class_mailer( $appCore);
					
					// Send E-Mail
					$mailer -> sendEmail(
						$this -> api -> user_email,
						$this -> api -> user_name,
						$appCore -> lang['email_register_title'],
						class_strings::parseMailText( file_get_contents( CACHE_PATH . 'lang_' . $this -> api -> user_language . '/email_register_adm.txt'), array(
							'{USER_NAME}' 			=> $this -> api -> user_name,
							'{USER_LOGIN}' 			=> $this -> api -> user_login,
							'{USER_PASSWORD}' 		=> $appCore -> _POST( 'user_password'),
							'{USER_GROUP}' 			=> $this -> groups[$this -> api -> group_id],
							'{BOARD_NAME}' 			=> $appCore -> conf['board_name'],
							'{BOARD_LOGIN_URL}' 	=> $appCore -> emailLink( 'act=login', $appCore -> lang['login_form_login']),
							'{BOARD_URL}' 			=> $appCore -> emailLink()
						)));
				}
			}
		}
		else
		{
			// Draw Form
			$this -> drawForm();
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws Form
	 *
	 */
	
	function drawForm(){
	
		// Load fields templates
		$this -> appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
				
		// Cache output
		ob_start();
				
		// Open Form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=new_user'));
		echo $this -> appCore -> drawHidden( 'update', 1);
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['acp_new_user']);
		
		// Draw fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_user_form_login'], $this-> appCore -> drawTextInput( 'user_login', $this -> api -> user_login));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_user_form_name'], $this-> appCore -> drawTextInput( 'user_name', $this -> api -> user_name));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_user_form_password'], $this-> appCore -> drawPassInput( 'user_password', $this -> appCore -> _POST( 'user_password')));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_user_form_email'], $this-> appCore -> drawTextInput( 'user_email', $this -> api -> user_email));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_user_form_group'], $this-> appCore -> drawList( 'user_group', $this -> groups, $this -> api -> group_id));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_user_form_notify'], $this-> appCore -> drawYesNo( 'user_notify', $this -> appCore -> _POST( 'user_notify')), $this -> appCore -> lang['acp_user_form_notify_expl']);
		
		// Close Form
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['new_user_form_submit']);
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> closeForm();
		
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['acp_new_user'], array(
			'' => $this -> appCore -> lang['acp_new_user']
		), ob_get_clean());
			
	}

}